﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Server
{
    public class Server
    {
        private DB _db = new DB();

        public int Auth(String email, String password, ServiceDevice version) 
        {
            UserEntity u = _db.getUser(email, password);
            if (u == null)
                return (int)ServiceStatus.WrongParameter;
            else
            {
                SessionEntity s = _db.getSession(u);
                RefreshAllRssFeed(u);
                if (s == null)
                {
                    s = _db.createSession(u, version);
                    return s.id;
                }
                else
                {
                    if (s.version == (int)version)
                        return s.id;
                    else
                        return (int)ServiceStatus.ConnectedOnAnotherDevice;
                }
            }
        }

        public SessionEntity getSession(int session_id)
        {
            return _db.getSession(session_id);
        }

        public int closeSession(int session_id)
        {
            if (_db.deleteSession(session_id))
                return 1;
            return 0;
        }

        public List<RssFeed> listRssFeed(UserEntity u, bool _public)
        {
            if (!_public)
                return _db.getChannels(u);
            else
                return _db.getPublicChannels(u);
        }

        public List<RssFeedItem> listRssFeedItem(RssFeed rss_feed, UserEntity u)
        {
            return _db.getItems(rss_feed, u);
        }

        public List<RssFeedItem> listLastRssFeedItem(UserEntity u, bool no_read)
        {
            return _db.getLastItems(u, no_read);
        }

        public RssFeed Subscribe(RssFeed rss_feed, UserEntity u)
        {
            RegistrationEntity registration = _db.getRegistration(u, rss_feed);
            if (registration == null)
            {
                ChannelEntity chan = _db.getChannel(rss_feed.id);
                if (chan != null)
                {
                    _db.createRegistration(rss_feed, u);
                    return ConvertEntityModel.ToRssFeed(chan);
                }
                else
                {
                    RssFeedSync sync = new RssFeedSync(rss_feed.url);
                    rss_feed = sync.Save();
                    _db.createRegistration(rss_feed, u);
                    return rss_feed;
                }
            }
            return rss_feed;
        }

        public RssFeed RefreshRssFeed(RssFeed rss_feed)
        {
            RssFeedSync sync = new RssFeedSync(rss_feed.url);
            rss_feed = sync.Refresh();
            return rss_feed;
        }

        private void RefreshAllRssFeed(UserEntity u)
        {
            List<RssFeed> rss_feeds = listRssFeed(u, false);
            foreach(RssFeed rss_feed in rss_feeds)
            {
                RefreshRssFeed(rss_feed);
            }
        }

        public RssFeedItem ReadState(UserEntity u, RssFeedItem item, bool read)
        {
            ItemStateEntity state = _db.getItemState(item, u);
            if (state == null)
            {
                state = _db.createItemState(item, u, read);
                item.read = read;
                return item;
            }
            else
            {
                state = _db.updateItemState(state, read);
                item.read = read;
                return item;
            }

        }
    }
  
}